package autokeyboard.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import autokeyboard.data.ComboBoxModel;
import autokeyboard.data.PicturePanel;
import autokeyboard.handler.ControlsDialogEvent;

@SuppressWarnings({ "serial", "rawtypes", "unchecked" })
public class ControlsDialog extends JDialog
{
	private JPanel				panelOption	= new JPanel();
	private JPanel				panelButton	= new JPanel();

	private JLabel				lblStartKey	= new JLabel();
	private JLabel				lblPauseKey	= new JLabel();
	private JLabel				lblStopKey	= new JLabel();

	private JComboBox			cbbStartKey	= new JComboBox();
	private JComboBox			cbbPauseKey	= new JComboBox();
	private JComboBox			cbbStopKey	= new JComboBox();

	private ComboBoxModel		model		= new ComboBoxModel();

	private JButton				btnOK		= new JButton();
	private JButton				btnCancel	= new JButton();

	private ControlsDialogEvent	event		= new ControlsDialogEvent();

	private PicturePanel		picPanel	= new PicturePanel();

	public ControlsDialog(JFrame frame)
	{
		super(frame, true);
		initComponent();
		this.setSize(220, 230);
		this.setTitle("Controls Setting");
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setIconImage(frame.getIconImage());
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}

	private void initComponent()
	{
		setComponentProperties();

		setMainLayout();
		setLayoutPanelButton();
		setLayoutPanelOption();

		keyEvent();
		buttonEvent();
		comboboxEvent();
	}

	private void setMainLayout()
	{
		this.add(picPanel);
		picPanel.setLayout(new BorderLayout());
		picPanel.add(panelOption, BorderLayout.CENTER);
		picPanel.add(panelButton, BorderLayout.SOUTH);
		
		picPanel.path = "/Background/ControlsSetting.png";
		picPanel.repaint();
	}

	private void setLayoutPanelOption()
	{
		panelOption.setLayout(new FlowLayout(FlowLayout.LEFT));
		panelOption.setOpaque(false);
		panelOption.add(lblStartKey);
		panelOption.add(cbbStartKey);
		panelOption.add(lblPauseKey);
		panelOption.add(cbbPauseKey);
		panelOption.add(lblStopKey);
		panelOption.add(cbbStopKey);
	}

	private void setLayoutPanelButton()
	{
		panelButton.setLayout(new FlowLayout(FlowLayout.RIGHT));
		panelButton.setOpaque(false);
		panelButton.add(btnOK);
		panelButton.add(btnCancel);
	}

	private void setComponentProperties()
	{
		panelOption.setBorder(BorderFactory.createTitledBorder(""));
		panelOption.setPreferredSize(new Dimension(120, 120));
		panelButton.setPreferredSize(new Dimension(120, 50));

		setLabelProperties();
		setComboBoxModel();
		setButtonProperties();

	}

	private void setLabelProperties()
	{
		lblStartKey.setFont(new Font("Calibri", 1, 20));
		lblStartKey.setForeground(Color.white);
		lblStartKey.setText("Start Key  ");

		lblPauseKey.setFont(lblStartKey.getFont());
		lblPauseKey.setForeground(Color.white);
		lblPauseKey.setText("Pause Key");

		lblStopKey.setFont(lblStartKey.getFont());
		lblStopKey.setForeground(Color.white);
		lblStopKey.setText("Stop Key  ");
	}

	private void setButtonProperties()
	{
		btnOK.setFont(new Font("Calibri", 0, 20));
		btnOK.setText("OK");
		btnCancel.setFont(new Font("Calibri", 0, 20));
		btnCancel.setText("Cancel");
	}

	/**
	 * This method will set default item in all comboBox
	 */
	private void setComboBoxModel()
	{
		String[] cbbKeyItem = { "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12" };

		cbbStartKey.setModel(model.model(cbbKeyItem));
		cbbPauseKey.setModel(model.model(cbbKeyItem));
		cbbStopKey.setModel(model.model(cbbKeyItem));

		cbbStartKey.setSelectedIndex(0);
		cbbPauseKey.setSelectedIndex(1);
		cbbStopKey.setSelectedIndex(2);

		String[] listNumber = new String[10];
		for (int i = 0; i < listNumber.length; i++)
		{
			listNumber[i] = (i + 1) + "";
		}

		cbbStartKey.setPreferredSize(new Dimension(80, 35));
		cbbPauseKey.setPreferredSize(new Dimension(80, 35));
		cbbStopKey.setPreferredSize(new Dimension(80, 35));
	}

	/**
	 * This method will add event key pressed for all component in this window
	 */
	private void keyEvent()
	{
		JComboBox[] listComboBox = { cbbPauseKey, cbbStartKey, cbbStopKey };
		JButton[] listButton = { btnOK, btnCancel };

		for (final JComboBox comboBox : listComboBox)
		{
			comboBox.addKeyListener(new KeyAdapter()
			{
				@Override
				public void keyPressed(KeyEvent evt)
				{
					if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
					{
						dispose();
					}
				}
			});
		}

		for (final JButton button : listButton)
		{
			button.addKeyListener(new KeyAdapter()
			{
				@Override
				public void keyPressed(KeyEvent evt)
				{
					if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
					{
						dispose();
					}
				}
			});
		}
	}

	/**
	 * This method will add event for all your button
	 */
	private void buttonEvent()
	{
		btnOK.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});

		btnCancel.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
	}

	/**
	 * This method will add selection event to your all comboBox
	 */
	private void comboboxEvent()
	{
		JComboBox[] listComboBox = { cbbStartKey, cbbPauseKey, cbbStopKey };

		for (final JComboBox combobox : listComboBox)
		{
			combobox.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent e)
				{
					event.isSameIndexComponent(cbbStartKey, cbbPauseKey, cbbStopKey);
				}
			});
		}
	}
}
